package com.sls.web.manager.action.game;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.common.FilterStringSpaces;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.Supply;
import com.sls.web.manager.game.querybean.SupplyQuery;
import com.sls.web.manager.game.service.SupplyManagerService;
import com.sls.web.manager.page.Pagination;

public class SupplyManagerAction extends SuperAction {

	private static final long serialVersionUID = 1L;

	@Resource(name = "supplyManagerService")
	private SupplyManagerService supplyManagerService;
	private String supplyId;
	private String supplyName;
	private Supply supply;
	private String supplyProperty;
	private String keyword;
	private SupplyQuery supplyQuery;
	private Pagination<Supply> page;
	private String supId;
	
	/**
	 * 添加
	 * 
	 * @return
	 */
	public String add() {
		return INPUT;
	}

	/**
	 * 保存
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() throws Exception {
		try {
			supply.setSupplyState(Constants.COOPERATE);
			boolean result = supplyManagerService.add(supply,
					getCurrentOperator());
			if (!result) {
				return ERROR;
			} else {
				redirectionUrl = "supply_manager!list.htm";
			}
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}

	/**
	 * 列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		try {
			if (supplyQuery == null) {
				supplyQuery = new SupplyQuery();
				supplyQuery.setPageSize(Constants.INIT_PAGESIZE);
				supplyQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
			}
			String supplyId = supplyQuery.getSupplyId();
			String supplyName = supplyQuery.getSupplyName();
			supplyId = FilterStringSpaces.checkStringHasSpace(supplyId);
			supplyName = FilterStringSpaces.checkStringHasSpace(supplyName);
			supplyQuery.setSupplyId(supplyId);
			supplyQuery.setSupplyName(supplyName);
			page = supplyManagerService.querySupply(supplyQuery,
					getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		return LIST;
	}

	/**
	 * 编辑
	 * 
	 * @return
	 * @throws Exception
	 */
	public String toEdit() throws Exception {
		try {
			supply = supplyManagerService.querySupplyById(supplyId,
					getCurrentOperator());
			if (supply == null) {
				return ERROR;
			}
		} catch (ManagerException e) {
			return ERROR;
		}

		return INPUT;
	}

	/**
	 * 编辑
	 * 
	 * @return
	 * @throws Exception
	 */
	public String edit() throws Exception {

		try {
			boolean result = supplyManagerService.update(supply,
					getCurrentOperator());
			if (!result) {
				 return ERROR;
			} else {
				if (supplyQuery == null) {
					supplyQuery = new SupplyQuery();
					supplyQuery.setPageSize(Constants.INIT_PAGESIZE);
					supplyQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
				}
				if (StringUtils.isNotEmpty(supId)) {
					supplyQuery.setSupplyId(supId);
				}
				if (StringUtils.isNotEmpty(supplyName)) {
					supplyQuery.setSupplyName(supplyName);
				}
				return list();
			}
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
			redirectionUrl = "supply_manager!list.htm";
			return SUCCESS;
		}
	}

	public String getSupId() {
		return supId;
	}

	public void setSupId(String supId) {
		this.supId = supId;
	}

	public String getSupplyId() {
		return supplyId;
	}

	public void setSupplyId(String supplyId) {
		this.supplyId = supplyId;
	}

	public Supply getSupply() {
		return supply;
	}

	public void setSupply(Supply supply) {
		this.supply = supply;
	}

	public String getSupplyProperty() {
		return supplyProperty;
	}

	public void setSupplyProperty(String supplyProperty) {
		this.supplyProperty = supplyProperty;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public SupplyQuery getSupplyQuery() {
		return supplyQuery;
	}

	public void setSupplyQuery(SupplyQuery supplyQuery) {
		this.supplyQuery = supplyQuery;
	}

	public Pagination<Supply> getPage() {
		return page;
	}

	public void setPage(Pagination<Supply> page) {
		this.page = page;
	}

	public String getSupplyName() {
		return supplyName;
	}

	public void setSupplyName(String supplyName) {
		this.supplyName = supplyName;
	}

}
